package com.example.chenjun.myapplication.net.okhttp;

import android.os.Environment;
import android.support.v4.os.EnvironmentCompat;

import com.example.chenjun.myapplication.app.MyApplication;
import com.example.chenjun.myapplication.net.NetController;
import com.example.chenjun.myapplication.net.okhttp.request.GetRequest;
import com.example.chenjun.myapplication.net.okhttp.request.MultiRequest;
import com.example.chenjun.myapplication.net.okhttp.request.PostFormRequest;

import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class OkhttpUtil {

    private static final long TIMEOUT = 60;
    private static final int SIZE = 10 * 1024 * 1024;

    private static OkHttpClient client;

    public static OkHttpClient getOkHttpSingleInstance() {
        return getOkHttpSingleInstance(TIMEOUT, TimeUnit.SECONDS);
    }

    public static OkHttpClient getOkHttpSingleInstance(long timeMillion, TimeUnit unit) {
        OkHttpClient.Builder builder;
        if (client == null) {
            builder = new OkHttpClient.Builder();
            builder.connectTimeout(timeMillion, unit);
            builder.readTimeout(timeMillion, unit);
            builder.writeTimeout(timeMillion, unit);
            builder.cache(new Cache(cacheFile(), SIZE));
            builder.addInterceptor(new LogIntercept());
        } else {
            if (client.connectTimeoutMillis() != timeMillion) {
                builder = client.newBuilder();
                builder.connectTimeout(timeMillion, unit);
                builder.readTimeout(timeMillion, unit);
                builder.writeTimeout(timeMillion, unit);
            } else {
                return client;
            }
        }
        return builder.build();
    }

    private static File cacheFile() {
        return MyApplication.getInstance().getCacheDir();
    }

    public static Call newCall(NetController controller) {
        Map<String, String> params = controller.getParams();
        File file = controller.getFile();
        Map<String, File> files = controller.getFiles();
        Map<String, String> headers = controller.getHeaders();
        String tag = controller.getTag();
        String url = controller.getUrl();
        NetController.METHOD method = controller.getMethod();
        Request request = null;
        switch (method) {
            case GET:
                request = new GetRequest(params, headers, url, tag, method.name()).buildRequest();
                break;
            case POST:
                if (file == null && (files == null || files.isEmpty())){
                    request = new PostFormRequest(params, headers, url, tag).buildRequest();
                }else {
                    request  = new MultiRequest(params, headers, url, tag, file, files).buildRequest();
                }
                break;
            default:
                request = new PostFormRequest(params, headers, url, tag).buildRequest();
                break;
        }
        return getOkHttpSingleInstance().newCall(request);
    }
}
